package org.apache.lorry.zookeeper.cluster;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@Slf4j
public class Task implements Serializable {
    private String name;

    private Integer num;

    private CountDownLatch latch = new CountDownLatch(1);

    public void execute(){
        log.info("name : {} begin say ...... ", name);
        try {
            TimeUnit.SECONDS.sleep(num);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("name : {} end say ...... ", name);
    }

    public Task(String name, Integer num) {
        this.name = name;
        this.num = num;
    }

    public void await(){
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void done() {
        latch.countDown();
    }

    public String getName() {
        return name;
    }

    public Integer getNum() {
        return num;
    }

    public static void main(String[] args) {
        Task task = new Task("abc", 30);
        System.out.println(JSON.toJSON(task));
    }
}
